<template>
  <div>
    <div class="gva-search-box">
      <el-form ref="searchForm" :inline="true" :model="searchInfo">
        <el-form-item label="姓名">
          <el-input v-model="searchInfo.username" placeholder="姓名" />
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="searchInfo.phone" placeholder="手机号码" />
        </el-form-item>
        <el-form-item label="审核状态">
          <el-select v-model="searchInfo.state" placeholder="请选择审核状态">
            <el-option
              v-for="item in stateOption"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="用户身份">
          <el-select v-model="searchInfo.status" placeholder="请选择用户身份">
            <el-option
              v-for="item in selectOption"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="search" @click="onSubmit">查询</el-button>
          <el-button icon="refresh" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="gva-table-box">
      <el-table :data="tableData" row-key="id">
        <el-table-column align="left" label="用户ID" min-width="150" prop="id" />
        <el-table-column align="left" label="姓名" min-width="150" prop="username" />
        <el-table-column align="left" label="手机号码" min-width="150" prop="phone" />
        <el-table-column align="left" label="身份证ID" min-width="150" prop="id_card" />
        <el-table-column align="left" label="身份证正面" min-width="150">
          <template #default="scope">
            <img :src="scope.row.front_card_img" alt class="avatar" />
          </template>
        </el-table-column>
        <el-table-column align="left" label="身份证反面" min-width="150">
          <template #default="scope">
            <img :src="scope.row.opposite_card_img" alt class="avatar" />
          </template>
        </el-table-column>
        <el-table-column align="left" label="驾驶证照片" min-width="150" v-if="filterType===2">
          <template #default="scope">
            <img :src="scope.row.driver_img" alt class="avatar" />
          </template>
        </el-table-column>
        <el-table-column
          align="left"
          label="驾驶证ID"
          min-width="150"
          v-if="filterType===2"
          prop="driver_id_card"
        />
        <el-table-column
          align="left"
          label="驾驶证过期时间"
          min-width="150"
          v-if="filterType===2"
          prop="driver_expire"
        />
        <el-table-column align="left" label="营业执照图片" min-width="150" v-if="filterType===4">
          <template #default="scope">
            <img :src="scope.row.business_license_img" alt class="avatar" />
          </template>
        </el-table-column>
        <el-table-column
          align="left"
          label="营业执照过期时间"
          min-width="150"
          v-if="filterType===4"
          prop="business_license_expire"
        />
        <el-table-column align="left" label="用户身份" min-width="150">
          <template #default="scope">
            <span>{{ displayStatus(scope.row.status) }}</span>
          </template>
        </el-table-column>
        <el-table-column align="left" label="审核状态" min-width="150">
          <template #default="scope">
            <span>{{ displayState(scope.row.state) }}</span>
          </template>
        </el-table-column>
        <el-table-column align="left" label="操作" min-width="150">
          <template #default="scope">
            <el-button class="edit" @click="fnChange(scope.row)">审核</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="gva-pagination">
        <el-pagination
          :current-page="page"
          :page-size="page_size"
          :page-sizes="[10, 30, 50, 100]"
          :total="totals"
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
    <el-drawer
      v-model="dialogFormVisible"
      size="30%"
      :before-close="closeDialog"
      :show-close="false"
    >
      <template #header>
        <span class="text-lg">审核</span>
        <div>
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="changeState">确 定</el-button>
        </div>
      </template>
      <el-form ref="platForm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审核" prop="status">
          <el-radio-group v-model="form.state">
            <el-radio :value="1">身份证审核</el-radio>
            <el-radio :value="2">审核通过</el-radio>
            <el-radio :value="3">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>
<script setup>
import { ref, onMounted, computed } from "vue";

import { subscribeList, subscribeUpdate } from "@/api/subscribe";
import { ElMessage } from "element-plus";

let searchInfo = ref({
  username: "",
  phone: "",
  status: 1,
  state: ""
});
let tableData = ref([]);
let totals = ref(0);
let page = ref(1);
let page_size = ref(10);
let dialogFormVisible = ref(false);
let form = ref({
  state: 1
});
const rules = ref({
  status: [{ required: true, message: "请选择用户状态", trigger: "blur" }]
});
let filterType = ref(1);
const selectOption = [
  {
    id: 1,
    name: "普通用户"
  },
  {
    id: 2,
    name: "骑手"
  },
  {
    id: 3,
    name: "网点"
  },
  {
    id: 4,
    name: "商家"
  }
];
const stateOption = [
  {
    id: 0,
    name: "待审核"
  },
  {
    id: 1,
    name: "身份证审核"
  },
  {
    id: 2,
    name: "审核通过"
  },
  {
    id: 3,
    name: "驳回"
  }
];

onMounted(() => {
  getList();
});

const displayStatus = computed(() => {
  return val => {
    if (val === 1) {
      return "普通用户";
    } else if (val === 2) {
      return "骑手";
    } else if (val === 3) {
      return "网点";
    } else if (val === 4) {
      return "商家";
    }
  };
});
const displayState = computed(() => {
  return val => {
    console.log(val);
    if (val === 0) {
      return "待审核";
    } else if (val === 1) {
      return "身份证审核";
    } else if (val === 2) {
      return "审核通过";
    } else if (val === 3) {
      return "驳回";
    }
  };
});
const fnChange = row => {
  form.value = JSON.parse(JSON.stringify(row));
  dialogFormVisible.value = true;
};
const closeDialog = () => {
  dialogFormVisible.value = false;
};
const changeState = row => {
  let params = {
    id: form.value.id,
    phone: form.value.phone,
    username: form.value.username,
    state: form.value.state
  };
  subscribeUpdate(params).then(response => {
    if (response.code === 200) {
      ElMessage({
        message: response.msg ? response.msg : "操作成功",
        type: "success"
      });
      closeDialog();
      page.value = 1;
      page_size.value = 10;
      getList();
    }
  });
};
const getList = () => {
  let params = {
    page: page.value,
    page_size: page_size.value,
    username: searchInfo.value.username,
    phone: searchInfo.value.phone ? Number(searchInfo.value.phone) : undefined,
    state: searchInfo.value.state,
    status: searchInfo.value.status
  };
  subscribeList(params).then(response => {
    console.log(response);
    filterType.value = searchInfo.value.status;
    if (response.code === 200) {
      tableData.value = response.data.rows || [];
      totals.value = response.data.total || 0;
    }
  });
};
const handleCurrentChange = val => {
  page.value = val;
  getList();
};
const handleSizeChange = val => {
  page_size.value = val;
  getList();
};
const onSubmit = () => {
  page.value = 1;
  page_size.value = 10;
  getList();
};
const onReset = () => {
  searchInfo.value = {
    username: "",
    phone: "",
    status: 1,
    state: ""
  };
  page.value = 1;
  page_size.value = 10;
  totals.value = 0;
  getList();
};
</script>
<style lang="scss" scoped>
.avatar {
  width: 60px;
  height: 60px;
  margin-right: 10px;
}
</style>